package com.lcc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lcc.mapper.EmpMapper;
import com.lcc.pojo.Emp;
import com.lcc.pojo.PageBean;
import com.lcc.service.EmpService;
import com.lcc.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageBean selectByPage(Integer page, Integer pageSize, String name, Short gender,
                                 LocalDate begin, LocalDate end) {
//        1. 设置分页参数
        PageHelper.startPage(page,pageSize);
//        2. 执行查询
        List<Emp> empList = empMapper.list(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>) empList;
//        3. 封装成bean对象
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    /**
     * 批量删除员工
     * @param ids
     */
    @Override
    public void deleteEmp(List<Integer> ids) {
        empMapper.delete(ids);
    }

    /**
     * 新增员工
     * @param emp
     */
    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    /**
     * 根据ID查询员工
     * @param id
     * @return
     */
    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    /**
     * 补充emp的更新时间
     * 更新员工信息
     * @param emp
     */
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    /**
     * 登录查询
     * @param emp
     * @return
     */
    @Override
    public Emp login(Emp emp) {
        return empMapper.getByUsernameAndPassword(emp);
    }

    /*@Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //1.设置分页参数
        PageHelper.startPage(page, pageSize);

        //2.执行查询
        List<Emp> empList = empMapper.list(name, gender, begin, end);//查询完之后需要强转为page类型
        Page<Emp> p = (Page<Emp>) empList;

        //3.将数据总数量和列表数据封装到PageBean完成后返回给controller
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }*/
}
